package com.canbot.u05.activity.agenda

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.canbot.u05.R
import com.canbot.u05.activity.agenda.adapter.AgendaTypeAdapter
import com.canbot.u05.activity.agenda.bean.AgendaTypeBean
import com.canbot.u05.activity.agenda.bean.AgendaWorkDayBean
import com.canbot.u05.activity.agenda.bean.DateBean
import com.canbot.u05.activity.agenda.calendar.getDate
import com.canbot.u05.activity.agenda.calendar.translateWeekIndex
import com.canbot.u05.activity.agenda.dialog.DateDialog
import com.canbot.u05.activity.agenda.interfaces.IAgendaTypeLisenter
import com.canbot.u05.activity.agenda.interfaces.IDateSelect
import com.canbot.u05.activity.presidemode.newcode.BaseHostActivity
import com.canbot.u05.utils.ToastTools
import kotlinx.android.synthetic.main.activity_agenda_work_day_more.*
import kotlinx.android.synthetic.main.activity_agenda_work_day_one.*
import java.util.*
import kotlin.collections.ArrayList

/**
 * @author yinxiaowei
 * @date 2019/5/31
 */
class AgendaWorkChoiceActivity : BaseHostActivity(), View.OnClickListener {
    private var workType: Int = 0
    private lateinit var adapter: AgendaTypeAdapter
    private val dataList = ArrayList<AgendaTypeBean>()
    private var agendaWorkDayBean: AgendaWorkDayBean = AgendaWorkDayBean()
    override fun initEventData() {
        if (workType == WORK_ONE) {
            work_day_today.setOnClickListener(this)
            work_day_tomorrow.setOnClickListener(this)
            work_day_afterTomorrow.setOnClickListener(this)
            work_someday.setOnClickListener(this)
            updateSelect()

        } else {
            val weekList: ArrayList<DateBean> = ArrayList()
            day_repeat.setOnClickListener {
                dataList.filter {
                    it.isSelect
                }.count().let {
                    if (it > 0) {
                        if (weekList.size > 0) {
                            agendaWorkDayBean.data.clear()
                            agendaWorkDayBean.data.addAll(weekList)
                        }
                        exit()
                    } else {
                        ToastTools.newInstance(this.applicationContext).showText(getString(R.string.date_choice))
                    }
                }
            }
            val stringArray = resources.getStringArray(R.array.work_day)
            for (s in stringArray) {
                val element = AgendaTypeBean()
                element.type = s
                dataList.add(element)
            }
            updateSelect()
            recycleView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
            adapter = AgendaTypeAdapter(dataList, this, object : IAgendaTypeLisenter {
                override fun onSelect(pos: Int) {
                    if (pos == (dataList.size - 1)) {
                        showDateDialog(true)
                    } else { // 星期怎么转换
                        dataList[dataList.size - 1].isSelect = false
                        dataList[pos].isSelect = !dataList[pos].isSelect
                        adapter.notifyDataSetChanged()
                        val dateBean = DateBean()
                        dateBean.isRepeat = true
                        dateBean.week = translateWeekIndex(pos) // 这里先存星期，最后保存时，根据时间段，进行转换
                        if (dataList[pos].isSelect) {
                            if (!weekList.contains(dateBean)) {
                                weekList.add(dateBean)
                            }
                        } else {
                            if (weekList.contains(dateBean)) {
                                weekList.remove(dateBean)
                            }
                        }
                    }
                }
            })
            recycleView.adapter = adapter
        }
    }

    private fun updateSelect() {
        if (agendaWorkDayBean.data.size > 0) {
            if (workType == WORK_ONE) {
                val dateBean0 = getDateBean(0)
                if (dateBean0 == agendaWorkDayBean.data[0]) {
                    work_day_today.isSelected = true
                } else {
                    val dateBean1 = getDateBean(1)
                    if (dateBean1 == agendaWorkDayBean.data[0]) {
                        work_day_tomorrow.isSelected = true
                    } else {
                        val dateBean2 = getDateBean(2)
                        if (dateBean2 == agendaWorkDayBean.data[0]) {
                            work_day_afterTomorrow.isSelected = true
                        } else {
                            work_someday.isSelected = true
                        }
                    }
                }
            } else {
                agendaWorkDayBean.data.forEach {
                    if (it.week != -1) {// 星期
                        dataList[it.week - 1].isSelect = true
                    } else { // 日期
                        dataList[dataList.size - 1].isSelect = true
                    }
                }
            }
        }
    }


    override fun getLayoutResId(): Int {
        agendaWorkDayBean = intent.getSerializableExtra(DATA) as AgendaWorkDayBean
        workType = if (agendaWorkDayBean.dayOne) WORK_ONE else WORK_MORE
        return if (workType == WORK_ONE) {
            R.layout.activity_agenda_work_day_one
        } else {
            R.layout.activity_agenda_work_day_more //星期选择，可以重复
        }
    }

    override fun onBackPressed() {
        exit()
    }

    fun back(view: View) {
        exit()
    }

    private fun exit() {
        val intent = Intent()
        intent.putExtra(BaseHostActivity.DATA, agendaWorkDayBean)
        setResult(workType, intent)
        finish()
    }

    override fun onClick(v: View) {
        agendaWorkDayBean.data.clear()
        when (v) {
            work_day_today -> {
                updateInfo(v, 0)
            }
            work_day_tomorrow -> {
                updateInfo(v, 1)
            }
            work_day_afterTomorrow -> {
                updateInfo(v, 2)
            }
            work_someday -> {
                showDateDialog(false)
            }
        }
    }

    private fun updateInfo(v: View, count: Int) {
        resetState()
        v.isSelected = true
        val dateBean = getDateBean(count)
        agendaWorkDayBean.data.add(dateBean)
        exit()
    }

    private fun getDateBean(count: Int): DateBean {
        val calendar = Calendar.getInstance()
        calendar.time = Date()
        calendar.add(Calendar.DATE, count)
        val dateBean = DateBean()
        dateBean.date = getDate(calendar.timeInMillis)
        return dateBean
    }

    private fun showDateDialog(multiSelect: Boolean) {
        val tempList: ArrayList<DateBean> = ArrayList()
        var haveSelectDate = false
        val dateDialog = DateDialog(this, multiSelect,false, object : IDateSelect {
            override fun onSelect(isSelect: Boolean, data: Calendar) {
                if (multiSelect) { // 多选模式
                    val dateBean = DateBean()
                    dateBean.date = getDate(data.timeInMillis)
                    if (isSelect) {
                        if (!tempList.contains(dateBean)) {
                            tempList.add(dateBean)
                        }
                    } else {
                        if (tempList.contains(dateBean)) {
                            tempList.remove(dateBean)
                        }
                    }
                } else { // 单选
                    tempList.clear()
                    if (isSelect) {
                        val dateBean = DateBean()
                        dateBean.date = getDate(data.timeInMillis)
                        tempList.add(dateBean)
                    }
                }
                haveSelectDate = tempList.size > 0
            }
        })
        dateDialog.setiDialogBtnLisenter {
            if (workType == WORK_ONE) {
                if (haveSelectDate) {
                    agendaWorkDayBean.data.clear()
                    agendaWorkDayBean.data.addAll(tempList)
                    resetState()
                    work_someday.isSelected = true
                    exit()
                }
            } else {
                if (haveSelectDate) {
                    agendaWorkDayBean.data.clear()
                    agendaWorkDayBean.data.addAll(tempList)
                    dataList.forEach {
                        it.isSelect = false
                    }
                    dataList[dataList.size - 1].isSelect = true
                    adapter.notifyDataSetChanged()
                    exit()
                }
            }
        }
        dateDialog.show()
    }

    private fun resetState() {
        work_day_today.isSelected = false
        work_day_tomorrow.isSelected = false
        work_day_afterTomorrow.isSelected = false
        work_someday.isSelected = false
    }

    companion object {
        const val WORK_ONE = 1
        const val WORK_MORE = 2
    }
}
